<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Overtrue\Pinyin\Pinyin;
use DB;
use App\User;
use Redis;
use App\Car;

class IndexController extends Controller
{

    public function index(Request $req)
    {
        //获得下拉的城市列表
        $cities = $this->getCity();
        //获得车的所有品牌
        $brands = $this->getBrand();
        $selCity = [];
        if($req->city == ''){
            $wz = file_get_contents('http://api.map.baidu.com/location/ip?ak=KBpQ73maDT2SKK2yt6YHEqPgEwwWGSgD&coor=bd09ll');
            $city = json_decode($wz,true)['content']['address_detail']['city'];
            //从容器中获取一个品议转化拼音的实例
            $pinyin = app('pinyin');
            //将城市名转化为拼音数组
            $cityPy = $pinyin->convert($city);
            //将最后一个值"shi"弹出
            array_pop($cityPy);
	        //将最后一个值"市"弹出
			$city = mb_substr($city, 0, -1);
			//将数组合并为字符串
            $cityStr = implode('',$cityPy);
            // $head = strtoupper(substr($cityStr,0,1));
            $selCity['pinyin'] = $cityStr;
            $selCity['value'] = $city;

        } else {
            //  dd($req->city);
             $char = strtoupper(substr($req->city, 0, 1));
            //  dd($char);
             $selCity['pinyin'] = $req->city;
             $selCity['value'] = $cities[$char][$req->city];
        }
		$like = $this->youLike($selCity['value']);
		$new = $this->newCar($selCity['value']);
        return view('index',[
            'selCity'=>$selCity,
            'cities' => $cities,
            'brands1' => $brands['a-m'],
			'brands2' => $brands['n-z'],
			'likes' => $like,
			'news' => $new
            ]);
    }

    //获得下拉的城市列表
    private function getCity()
    {
        $cities = config('city');
        return $cities;
    }

    //获得车辆品牌
    private function getBrand()
    {
        $brand = [];
        $brand['a-m'] = config('brand1');
        $brand['n-z'] = config('brand2');
        return $brand;
    }


    //更新侧边栏手机号
    public function mobile(Request $request)
    {
        if ($request->user()) {
            $uid = $request->user()->uid;
            DB::table('users')->where('uid', $uid)->update(['mobile',$request->mobile]);
            return view('sellcar');
        } else {
            return view('auth.login');
        }
    }

	//获取到猜你喜欢的数据
    public function youLike($city)
	{
		// if (Redis::get('index.like')) {
		// 	//从Redis获取到猜你喜欢数据
		// 	$like = Redis::get('index.like');
		// 	//将序列化的字符串转换回php的值
		// 	$like = unserialize($like);
		// }
		$like = Car::where(['checkcar_city'=>$city,'check_status'=>1,'seller_status'=>1,'survey_status'=>1,'sale_status'=>1])
                ->join('survey', 'survey.car_id', '=', 'car.car_id')
				->take(8)
				->get();
		// Redis::setex('index.like', 60*60, serialize($like));
        // dd($like);
		return $like;
	}

	//获取到最新车辆的数据
    public function newCar($city)
	{
		// if (Redis::get('index.new')) {
		// 	//从Redis获取到最新车辆数据
		// 	$new = Redis::get('index.new');
		// 	//将序列化的字符串转换回php的值
		// 	$new = unserialize($new);
		// }
		$new = Car::where(['checkcar_city'=>$city,'check_status'=>1,'seller_status'=>1,'survey_status'=>1,'sale_status'=>1])
                ->join('survey', 'survey.car_id', '=', 'car.car_id')
				->orderBy('pubtime','desc')
				->take(8)
				->get();
		//Redis::setex('index.new', 60*60, serialize($new));
		return $new;
	}
}
